﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using kwzx.Data;
using kwzx.Common;

namespace kwzx
{
    public partial class kqtb : Form
    {
        DataSet1TableAdapters.K_DeviceIssueTableAdapter k_deviceIssue = new DataSet1TableAdapters.K_DeviceIssueTableAdapter();
        DataSet1TableAdapters.T_BMTableAdapter bm = new DataSet1TableAdapters.T_BMTableAdapter();
        DataSet1TableAdapters.T_GROUPTableAdapter group = new DataSet1TableAdapters.T_GROUPTableAdapter();
        DataSet1TableAdapters.CD_CardTypeTableAdapter cd_card = new DataSet1TableAdapters.CD_CardTypeTableAdapter();
        DataSet1TableAdapters.K_CardTableAdapter k_card = new DataSet1TableAdapters.K_CardTableAdapter();
        //  SPMTableAdapters.TB_USER_CUSTOMINFOTableAdapter tb_user = new SPMTableAdapters.TB_USER_CUSTOMINFOTableAdapter();
        //   SPMTableAdapters.TB_USER_CARDTableAdapter tb_user_card = new SPMTableAdapters.TB_USER_CARDTableAdapter();
        //  DataSet1TableAdapters.K_CarddataTableAdapter kcad = new DataSet1TableAdapters.K_CarddataTableAdapter();

        //  DataSet2TableAdapters.M_user_accgrpTableAdapter useraccg = new DataSet2TableAdapters.M_user_accgrpTableAdapter();

        string fkbk = "";
        int k_gs_id = 0;
        public kqtb(string fkbk1)
        {
            fkbk = fkbk1;
            InitializeComponent();
        }

        private void fk_Load(object sender, EventArgs e)
        {


            // TODO: 这行代码将数据加载到表“d_kq.K_SysArea”中。您可以根据需要移动或删除它。
            this.k_SysAreaTableAdapter.Fill(this.d_kq.K_SysArea);

            foreach (var a in d_kq.K_SysArea)
            {


                comboBox1.Items.Add(a.S_Name);
                comboBox2.Items.Add(a.S_Name);
                comboBox3.Items.Add(a.S_Name);
                comboBox4.Items.Add(a.S_Name);
            }
            // TODO: 这行代码将数据加载到表“d_kq.DataTable1”中。您可以根据需要移动或删除它。
            this.dataTable1TableAdapter.Fill(this.d_kq.DataTable1);
            cd_card.Fill(dataSet1.CD_CardType);
            DataTable group_tb = group.GetDataBy_sjjg(0);
            for (int g = 0; g < group_tb.Rows.Count; g++)
            {
                TreeNode tn = new TreeNode();
                tn = treeView1.Nodes.Add(group_tb.Rows[g]["JGNAME"].ToString());
                tn.Text = group_tb.Rows[g]["jc"].ToString();
                tn.Tag = -1;
                tn.ToolTipText = "jg";
                tn.ImageIndex = 0;
                tn.SelectedImageIndex = 0;
                addBMTree(group_tb.Rows[g]["GRPUP_ID"].ToString(), "0", tn);
            }
            //外协单位
            TreeNode tn1 = new TreeNode();
            tn1 = treeView1.Nodes.Add("外协单位");
            tn1.Text = "外协单位";
            tn1.Tag = "0";
            tn1.ToolTipText = "jg";
            tn1.ImageIndex = 0;
            tn1.SelectedImageIndex = 0;
            tn1.Checked = true;
            addJGTree(group_tb.Rows[0]["GRPUP_ID"].ToString(), tn1);
            treeView1.Nodes[0].ExpandAll();
            treeView1.SelectedNode = treeView1.Nodes[0].NextVisibleNode;

        }

        /// <summary>
        /// 递归加载addJGTree列表
        /// </summary>
        /// <param name="fatherid">节点的fatherid</param>
        /// <param name="pnode">该节点</param>
        private void addJGTree(string fatherid, TreeNode pnode)
        {
            DataTable dt = group.GetDataBy_sjjg(int.Parse(fatherid));
            DataView dvTree = new DataView(dt);
            dvTree.RowFilter = "[SJJG] = " + fatherid;
            foreach (DataRowView drv in dvTree)
            {
                TreeNode node = new TreeNode();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 3;
                if (pnode == null)
                {
                    node.Text = drv["jc"].ToString();
                    node.Tag = drv["GRPUP_ID"].ToString();    //作为节点标识符
                    node.ToolTipText = "jg";
                    treeView1.Nodes.Add(node);
                    addJGTree(drv["GRPUP_ID"].ToString(), node);//递归  该节点加载子节点
                }
                else
                {
                    node.Text = drv["jc"].ToString();
                    node.Tag = drv["GRPUP_ID"].ToString();//作为节点标识符
                    node.ToolTipText = "jg";
                    pnode.Nodes.Add(node);
                    addJGTree(drv["GRPUP_ID"].ToString(), node);//递归  该节点加载子节点
                }
            }
        }

        /// <summary>
        /// 递归加载addBMTree列表
        /// </summary>
        /// <param name="fatherid">节点的fatherid</param>
        /// <param name="pnode">该节点</param>
        private void addBMTree(string jgid, string fatherid, TreeNode pnode)
        {
            DataTable dt = bm.GetDataBy_grpup_id(int.Parse(jgid));
            //  DataTable dt = bm.GetDataBy_oderBMMC(int.Parse(jgid));
            DataView dvTree = new DataView(dt);
            dvTree.RowFilter = "[SJBM] = " + fatherid;
            foreach (DataRowView drv in dvTree)
            {
                TreeNode node = new TreeNode();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 3;
                if (pnode == null)
                {
                    node.Text = drv["BMMC"].ToString();
                    node.Tag = drv["DEPT_ID"].ToString();    //作为节点标识符
                    node.ToolTipText = "bm";
                    treeView1.Nodes.Add(node);
                    addBMTree(jgid, drv["DEPT_ID"].ToString(), node);//递归  该节点加载子节点
                }
                else
                {
                    node.Text = drv["BMMC"].ToString();
                    node.Tag = drv["DEPT_ID"].ToString();//作为节点标识符
                    node.ToolTipText = "bm";
                    pnode.Nodes.Add(node);
                    addBMTree(jgid, drv["DEPT_ID"].ToString(), node);//递归  该节点加载子节点
                }
            }
        }

        /// <summary>
        /// 树选中一结点时，在右侧的GridView中关联显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                int id = int.Parse(e.Node.Tag.ToString());
                string tooltiptext = e.Node.ToolTipText;

                if (tooltiptext.Equals("jg"))
                {
                    this.dataTable1TableAdapter.FillBy_group(d_kq.DataTable1, int.Parse(treeView1.SelectedNode.Tag.ToString()));
                }
                else if (tooltiptext.Equals("bm"))
                {
                    this.dataTable1TableAdapter.FillBy_dept_id(d_kq.DataTable1, int.Parse(treeView1.SelectedNode.Tag.ToString()));
                }

                dataTable1BindingSource.Filter = "";
            }
        }

        /// <summary>
        /// 级联人员表及卡表信息显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void user_cardBindingSource_PositionChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 搜索操作(查询功能)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string sql = " 1=1 ";
            if (this.textBox1.Text.ToString() != "")
            {
                sql += " and ZSXM like '%" + this.textBox1.Text.ToString() + "%'";
            }
            if (this.textBox2.Text.ToString() != "")
            {
                sql += "and sfzh like '%" + this.textBox2.Text.ToString() + "%'";
            }
            this.dataTable1TableAdapter.Fill(d_kq.DataTable1);
            dataTable1BindingSource.Filter = sql;

            // user_cardBindingSource_PositionChanged(sender, e);
            this.user_cardDataGridView.Refresh();
        }

        private void user_cardDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (user_cardDataGridView.CurrentRow == null)
            {
                return;
            }
            if (e.RowIndex != -1)
            {
                System.Windows.Forms.DataGridViewCheckBoxCell cell = ((System.Windows.Forms.DataGridViewCheckBoxCell)(this.user_cardDataGridView[0, e.RowIndex]));
                cell.Value = cell.Value == null ? "True" : cell.Value.ToString() == "True" ? "False" : "True";
                //if (SelectedCells())
                //{

                //    IsSelect = true;
                //}
            }
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < user_cardDataGridView.Rows.Count; i++)
            {
                user_cardDataGridView.Rows[i].Cells[0].Value = true;
            }
        }

        private void 反选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < user_cardDataGridView.Rows.Count; i++)
            {
                user_cardDataGridView.Rows[i].Cells[0].Value = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "")
            {
                MessageBox.Show("请选择一个场所，放便数据下发");
                return;
            }
            kq.HWATTTableAdapters.KQZ_BrchTableAdapter kqbm = new kq.HWATTTableAdapters.KQZ_BrchTableAdapter();
            kq.HWATTTableAdapters.KQZ_EmployeeTableAdapter kempe = new kq.HWATTTableAdapters.KQZ_EmployeeTableAdapter();

            for (int i = 0; i < user_cardDataGridView.Rows.Count; i++)
            {
                if (user_cardDataGridView.Rows[i].Cells[0].Value != null)
                {


                    if (user_cardDataGridView.Rows[i].Cells[0].Value.ToString() == "True")
                    {
                        string uby = user_cardDataGridView.Rows[i].Cells["UBY"].Value.ToString();
                        string BMMC = user_cardDataGridView.Rows[i].Cells["BMMC"].Value.ToString();
                        int DEPT_id = int.Parse(user_cardDataGridView.Rows[i].Cells["DEPT_id"].Value.ToString());
                        int SXH = 0;
                        if (user_cardDataGridView.Rows[i].Cells["SXH"].Value.ToString() == "")
                        {
                        }
                        else
                        {
                            SXH = int.Parse(user_cardDataGridView.Rows[i].Cells["SXH"].Value.ToString());
                        }
                        int pid = 0;
                        string ATTENDDAY = "1111100";
                        DateTime AMBEGIN = DateTime.Now;
                        DataTable tbbm = kqbm.GetDataBy_BrchName(BMMC);
                        if (tbbm.Rows.Count < 1)// 部门不存在
                        {
                            if (uby == "-1")//正式员工
                            {
                                pid = 2;
                            }
                            else
                            {
                                pid = 4;
                            }

                            kqbm.Insert(long.Parse(DEPT_id.ToString()), long.Parse(pid.ToString()), BMMC, short.Parse(SXH.ToString()), ATTENDDAY, AMBEGIN, AMBEGIN, AMBEGIN, AMBEGIN, "", "");
                        }

                        DataTable tbbm1 = kqbm.GetDataBy_BrchName(BMMC);
                        if (tbbm1.Rows.Count > 0)
                        {

                            int BrchID = int.Parse(tbbm1.Rows[0]["BrchID"].ToString());
                            string BrchName = BrchID.ToString();	//varchar(200)
                            string EmployeeCode = user_cardDataGridView.Rows[i].Cells["dataGridViewTextBoxColumn1"].Value.ToString(); ;	//工号????
                            //17250
                            DataTable emptb = kempe.GetDataBy_EmployeeCode(EmployeeCode);
                            if (emptb.Rows.Count > 0)//重号后
                            {
                                for (int we = 1; we < 117250; we++)
                                {
                                    emptb = kempe.GetDataBy_EmployeeCode(we.ToString());
                                    if (emptb.Rows.Count < 1)
                                    {
                                        EmployeeCode = we.ToString();
                                        we = 99917250;
                                    }

                                }
                            }
                            string EmployeeName = user_cardDataGridView.Rows[i].Cells["dataGridViewTextBoxColumn4"].Value.ToString();//	varchar(250)//姓名
                            short Gender = 1;//	smallint
                            DateTime Birthday = DateTime.Now;//	datetime
                            string Nation = "";	//varchar(50)
                            string NATIVEPLACE = "";	//varchar(50)
                            string ZhengZhi = "";	//varchar(50)
                            string MARITALSTATUS = "";	//varchar(20)
                            string Health = "";	//varchar(20)
                            string Title = "";//	职位
                            string APOSITION = "";	//职称
                            string EDUCATION = "";	//varchar(50)
                            string School = "";	//varchar(100)
                            DateTime Graduation = DateTime.Now; 	//datetime

                            DateTime EnrollDate = DateTime.Now; 	//datetime
                            short Privilege = 0;//	smallint

                            string IDCard = user_cardDataGridView.Rows[i].Cells["dataGridViewTextBoxColumn20"].Value.ToString();  //	身份证
                            DataTable emptb2 = kempe.GetDataBy_idcard(IDCard);
                            if (emptb2.Rows.Count > 0)//重号后
                            {
                                //重号后不添加
                                continue;
                            }

                            string Comment = comboBox1.Text;//	备注   主场所
                            string AddrHome = comboBox2.Text;//	住址    场所1
                            string WorkPhone = comboBox3.Text;	//电话  场所2
                            string Mobile = comboBox4.Text;//	手机    场所3

                            string CardCode = "";//	卡号
                            short IsCheck = 1;//	smallint
                            short OnRule = 0;//	smallint
                            short OffRule = 0;//smallint
                            short CanOvertime = 1;//	smallint
                            short HaveFesta = 1;//	smallint
                            short DevType = 0;//	smallint

                            short RegisterType = 0;//	smallint

                            string EmployeeLogPwd = "123456";//	varchar(64)
                            string CheckType = "";//	varchar(30)
                            string OpendoorType = "";//	varchar(30)
                            short ModelNum = 18;//	smallint
                            short HavePwd = 0;//	smallint
                            short StaffStatus = 0;//	smallint
                            int LastCardID = 0;//	bigint
                            string Reserved1 = "";//	varchar(200)
                            string Reserved2 = "";//	varchar(200)
                            string Reserved3 = "";//	varchar(200)
                            string Reserved4 = "";//	varchar(200)
                            string DEVTYPESTR = "";//	varchar(50)
                            DateTime BEGINATTDATE = DateTime.Parse("2010-1-1 00:00:00");//	datetime
                            DateTime ENDATTDATE = DateTime.Parse("2099-12-31 00:00:00");//	datetime
                            string RealEmployeeCode = user_cardDataGridView.Rows[i].Cells["rybh"].Value.ToString(); //	工号

                            kempe.Insert(BrchID, BrchName, EmployeeCode, EmployeeName, Gender,
                    Birthday, Nation, NATIVEPLACE, ZhengZhi, MARITALSTATUS, Health, Title,
                    APOSITION, EDUCATION, School, Graduation, WorkPhone, EnrollDate, Privilege,
                    IDCard, AddrHome, CardCode, Mobile, IsCheck, OnRule, OffRule, CanOvertime,
                    HaveFesta, DevType, RegisterType, Comment, EmployeeLogPwd, CheckType,
                    OpendoorType, ModelNum, HavePwd, StaffStatus, LastCardID, Reserved1,
                    Reserved2, Reserved3, Reserved4, BEGINATTDATE, ENDATTDATE,
                    RealEmployeeCode);

                        }
                        else
                        {
                            MessageBox.Show(" 部门没找到");
                        }

                    }

                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            kq.D_kqTableAdapters.DataTable1TableAdapter user = new kq.D_kqTableAdapters.DataTable1TableAdapter();
            kq.HWATTTableAdapters.KQZ_EmployeeTableAdapter emp = new kq.HWATTTableAdapters.KQZ_EmployeeTableAdapter();
            DataTable usertb = user.GetData();

            for (int i = 0; i < usertb.Rows.Count; i++)
            {
                string sfzh = usertb.Rows[i]["sfzh"].ToString();
                string name = usertb.Rows[i]["ZSXM"].ToString();
                string rybh = usertb.Rows[i]["rybh"].ToString();
                DataTable emptb = emp.GetDataBy_EmployeeName(name);
                if (emptb.Rows.Count == 1)//没有重名
                {
                    string id = emptb.Rows[0]["EmployeeID"].ToString();
                    emp.UpdateQuery_sfzh(sfzh, rybh, long.Parse(id));
                }

            }
            MessageBox.Show("处理完成");

        }

        private void button5_Click(object sender, EventArgs e)
        {
            kq.D_kqTableAdapters.DataTable1TableAdapter user = new kq.D_kqTableAdapters.DataTable1TableAdapter();
            kq.HWATTTableAdapters.KQZ_EmployeeTableAdapter emp = new kq.HWATTTableAdapters.KQZ_EmployeeTableAdapter();
            kq.HWATTTableAdapters.KQZ_BrchTableAdapter bm = new kq.HWATTTableAdapters.KQZ_BrchTableAdapter();
            DataTable emptb = emp.GetData();
            for (int i = 0; i < emptb.Rows.Count; i++)
            {
                string sfzh = emptb.Rows[i]["IDCard"].ToString();
                string EmployeeID = emptb.Rows[i]["EmployeeID"].ToString();
                
                if (sfzh != "")
                {
                    DataTable usertb = user.GetDataBy_sfzh(sfzh);
                    string BMMC = usertb.Rows[0]["BMMC"].ToString();
                    DataTable bmtb = bm.GetDataBy_BrchName(BMMC);
                    if (bmtb.Rows.Count>0)
                    {
                        string bmid = bmtb.Rows[0]["BrchID"].ToString();
                        emp.UpdateQuery_bm(long.Parse(bmid), bmid,long.Parse( EmployeeID));
                    }


                }
            }
            MessageBox.Show("处理完成");
        }
    }
}
